﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.IO;
using System.Runtime.Serialization;
using System.Text;

namespace SharedCode.Common
{
    public static class ObjectClone
    {
        public static T Clone<T>(T obj)
        {
            DataContractSerializer serializer = new DataContractSerializer(obj.GetType());

            System.IO.MemoryStream ms = new System.IO.MemoryStream();
            serializer.WriteObject(ms, obj);

            byte[] bytes = ms.ToArray();
            ms.Close();
            string xml =  System.Text.Encoding.UTF8.GetString(bytes, 0, bytes.Length);

            var stream = new MemoryStream(Encoding.Unicode.GetBytes(xml));
            T theObject = (T)serializer.ReadObject(stream);
            stream.Close();
            return theObject;
        }
    }
}
